#include "mainwindow.h"

#include "GUI_SIM_Win32.h"
#include "GUI.h"
#include "simmsg.h"

class SimThread : public QThread{
    void run(){
        MainTask();
    }
};

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
    //左边布局
    auto *simWidget = new QWidget;
    simWidget->setMinimumSize(320,240);
    HWND simWidgetId = (HWND)simWidget->winId();
    SIM_GUI_Enable();
    SIM_GUI_Init((HINSTANCE)winId(), simWidgetId, NULL, "GUI Simulation");
    SIM_GUI_CreateLCDWindow(simWidgetId, 0, 0, 320, 240, 0);
    auto *simThread = new SimThread;
    simThread->start();

    //右边布局
    //右上
    //CH1
    auto *ch1Layout = new QVBoxLayout;
    auto *ch1DialV = new QSlider(Qt::Horizontal);
    auto *dialVLayout = new QHBoxLayout;
    dialVLayout->addWidget(new QLabel("偏移:"));
    dialVLayout->addWidget(ch1DialV);
    auto *ch1DialP = new QSlider(Qt::Horizontal);
    auto *dialPLayout = new QHBoxLayout;
    dialPLayout->addWidget(new QLabel("倍率:"));
    dialPLayout->addWidget(ch1DialP);
    ch1Layout->addLayout(dialVLayout);
    ch1Layout->addLayout(dialPLayout);
    auto *ch1GroupBox = new QGroupBox("CH1");
    ch1GroupBox->setLayout(ch1Layout);
    //Time
    auto *TimeLayout = new QVBoxLayout;
    auto *timeDialT = new QSlider(Qt::Horizontal);
    auto *timeTLayout = new QHBoxLayout;
    timeTLayout->addWidget(new QLabel("偏移:"));
    timeTLayout->addWidget(timeDialT);
    auto *timeDialP = new QSlider(Qt::Horizontal);
    auto *timePLayout = new QHBoxLayout;
    timePLayout->addWidget(new QLabel("倍率:"));
    timePLayout->addWidget(timeDialP);
    TimeLayout->addLayout(timeTLayout);
    TimeLayout->addLayout(timePLayout);
    auto *timeGroupBox = new QGroupBox("Time");
    timeGroupBox->setLayout(TimeLayout);

    //右侧总布局
    auto *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(ch1GroupBox);
    rightLayout->addWidget(timeGroupBox);

    //总布局
    auto *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(simWidget);
    mainLayout->addLayout(rightLayout);
    auto *centerWindow = new QWidget;
    centerWindow->setLayout(mainLayout);
    setCentralWidget(centerWindow);
    setMaximumSize(640, 258);
    setMinimumSize(640, 258);
    setWindowTitle("ucGUI示波器");

    //值初始化，并绑定事件
    ch1DialV->setValue(50);
    ch1DialP->setValue(50);
    timeDialT->setValue(50);
    timeDialP->setValue(50);
    auto *timer = new QTimer;
    timer->setInterval(100);
    timer->start();
    connect(timer, &QTimer::timeout, this, []{
        setMsg(100,1);
    });
    connect(ch1DialV, &QSlider::valueChanged, this, [](int value){
        setMsg(1, value - 50);
    });
    connect(timeDialT, &QSlider::valueChanged, this, [](int value){
        setMsg(2, value - 50);
    });
    connect(ch1DialP, &QSlider::valueChanged, this, [](int value){
        setMsg(3, value);
    });
    connect(timeDialP, &QSlider::valueChanged, this, [](int value){
        setMsg(4, value);
    });
}

MainWindow::~MainWindow(){
    SIM_GUI_Exit();
}


